﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

using Coding4Fun.Phone.Controls;
using Coding4Fun.Phone.Site.Controls;
using Microsoft.Devices;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using Microsoft.Xna.Framework.Media;

namespace Coding4Fun.SlideShow
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            Loaded += MainPage_Loaded;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void LaunchAbout_Click(object sender, EventArgs e)
        {
            var about = new Coding4FunAboutPrompt();
                
                about.Show(
                new []
                {
                    new AboutPersonItem{ AuthorName = "352 Media Group", Role = "dev &\ndesign", WebSiteUrl = "http://www.352media.com", EmailAddress = "info@352media.com"},
                    new AboutPersonItem{ AuthorName = "Clint Rutkas", Role = "dev", WebSiteUrl = "http://betterthaneveryone.com", EmailAddress = "clint@rutkas.com"},
                });
        }

        /// <summary>
        /// Event Handler that occurs when the PhoneApplicationPage control loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            var library = new MediaLibrary();

            // If debugging Create an Extra Album and throw duplicate albums onto the screen
            if (Microsoft.Devices.Environment.DeviceType == DeviceType.Emulator)
            {
                // Creates photos under the Saved Pictures Album for a total of two albums
                if (library.RootPictureAlbum.Albums.Count == 1)
                {
                    foreach (Picture picture in library.RootPictureAlbum.Albums[0].Pictures)
                    {
                        library.SavePicture(picture.Name, picture.GetImage());
                    }
                }
            }

            try
            {
                GetAlbums(library.RootPictureAlbum.Albums);
            }
            catch (InvalidOperationException ex0)
            {
                if (NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    MessageBox.Show("Please remove from Zune and \nrestart the application");
                }
            }

            Dispatcher.BeginInvoke(() => loadingScreen.Visibility = Visibility.Collapsed);
        }

        /// <summary>
        /// Gets Albums from Album collection and adds them to the listing
        /// </summary>
        /// <param name="albumCollection">Collection of Albums</param>
        private void GetAlbums(PictureAlbumCollection albumCollection)
        {
            var albums = new List<AlbumListItem>();

            for (var i = albumCollection.Count() - 1; i >= 0; i--)
            {
                var pictureAlbum = albumCollection[i];
           
                if (pictureAlbum.Albums.Count > 0)
                    GetAlbums(pictureAlbum.Albums);

                if (pictureAlbum.Pictures.Count == 0)
                    continue;

                var albumListItem = new AlbumListItem { Album = pictureAlbum };
                albumListItem.PictureSelected += albumListItem_PictureSelected;
                
                albums.Add(albumListItem);
            }

            items.ItemsSource = albums;
        }
        
        void albumListItem_PictureSelected(object sender, EventArgs e)
        {
            GC.Collect();
            NavigationService.Navigate(new Uri("/PlayerPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}